package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequestMapping("/list")
    public String list(Model model, QueryObject queryObject) {
        PageInfo systemDictionaryList = systemDictionaryService.list(queryObject);
        model.addAttribute("result", systemDictionaryList);
        return "systemDictionary/list";
    }

    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) { // 点击了修改按钮过来
            SystemDictionary systemDictionary = systemDictionaryService.get(id);
            model.addAttribute("systemDictionary", systemDictionary);
        }
        return "systemDictionary/input";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {
        System.out.println(systemDictionary);
        if (systemDictionary.getId() == null) { // 代表新增
            systemDictionaryService.save(systemDictionary);
        } else { // 代表修改
            systemDictionaryService.update(systemDictionary);
        }
        return new JsonResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        systemDictionaryService.delete(id);
        return new JsonResult();
    }
}
